﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.DBUtility;
using System.Data;
using System.IO;

namespace SkWeb.XDJS
{
    public partial class EYS_XDJS : System.Web.UI.Page
    {
        #region 变量

        #endregion 变量

        string JH;//井号
        string WJYMD;//完井液密度
        string JS;//井深
        string AQYL;//安全压力
        string WD;//温度

        #region 事件处理程序

        //页面加载事件
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //计算按钮点击事件
        protected void btn_Click(object sender, EventArgs e)
        {
            try
            {
                JiSuan();
                btn_Word.Visible = true;
            }
            catch (Exception ex)
            {
                lbl_err.Text = ex.Message.ToString();
            }
        }

        //导出到word按钮点击事件
        protected void btn_Word_Click(object sender, EventArgs e)
        {
            try
            {
                Download();
            }
            catch (Exception ex)
            {
                lbl_err.Text = ex.Message.ToString();
            }
        }

        #endregion 事件处理程序

        #region 函数集

        //计算结果
        public void JiSuan()
        {
            try
            {
                lbl_err.Text = "";
                if (!string.IsNullOrEmpty(txt_JH.Text.Trim()))
                {
                    JH = txt_JH.Text.Trim();
                }
                else
                {
                    lbl_err.Text = "井号不能为空！";
                    return;
                }
                if (!string.IsNullOrEmpty(txt_WJYMD.Text.Trim()))
                {
                    WJYMD = txt_WJYMD.Text.Trim();
                }
                else
                {
                    lbl_err.Text = "完井液密度不能为空！";
                    return;
                }
                if (!string.IsNullOrEmpty(txt_JS.Text.Trim()))
                {
                    JS = txt_JS.Text.Trim();
                }
                else
                {
                    lbl_err.Text = "井深不能为空";
                    return;
                }
                if (!string.IsNullOrEmpty(txt_AQYL.Text.Trim()))
                {
                    AQYL = txt_AQYL.Text.Trim();
                }
                else
                {
                    lbl_err.Text = "安全压力不能为空";
                    return;
                }
                if (!string.IsNullOrEmpty(txt_WD.Text.Trim()))
                {
                    WD = txt_WD.Text.Trim();
                }
                else
                {
                    lbl_err.Text = "温度不能为空";
                    return;
                }

                string sql = @"select JDZ from tb_eys_jqxzwd where wd=" + WD;
                DataSet ds = DbHelperOra.Query(sql.ToString());
                //剪切销值随温度降低百分率
                string WDJDS = ds.Tables[0].Rows[0]["JDZ"].ToString();
                double wdjdss = Convert.ToDouble(WDJDS);
                double wdjsss = wdjdss / 100;
                //剪切销的最小剪切值
                double Pmin = 3.09 * (1 - 0.045) * (1 - wdjsss);
                Pmin = Math.Round(Pmin, 2);
                //剪切销的最大剪切值
                double Pmax = 3.09 * (1 + 0.045) * (1 - wdjsss);
                Pmax = Math.Round(Pmax, 2);

                //起爆器所在位置垂直深度静压力
                //井深*完井液密度*0.0098
                double ρgh = Convert.ToDouble(JS) * Convert.ToDouble(WJYMD) * 0.0098;
                ρgh = Math.Round(ρgh, 2);
                //剪切销钉的数量为:n=(P1+P安)/Pmin P1 = ρgh
                double N = (ρgh + Convert.ToDouble(AQYL)) / Pmin;
                N = Math.Round(N, 0);
                //井口加压最大值 Ps = (N*Pmax)-P1
                double Ps = (N * Pmax) - ρgh;
                Ps = Math.Round(Ps, 2);
                //井口加压最小值        Px = (N*Pmin)-P1
                double Px = (N * Pmin) - ρgh;
                Px = Math.Round(Px, 2);
                //井口外加计算压力 YL = (Px*Ps)/2
                double YL = (Px + Ps) / 2;
                YL = Math.Round(YL, 2);
                string jieguo = @"
中间结果:</br></br>
1、剪切强度随温度降低百分数:"+WDJDS+"%</br>";
                jieguo+=@"
2、销钉最小压力值:"+Pmin.ToString()+"MPa</br>";
                jieguo += @"
3、销钉最大压力值:"+Pmax.ToString()+"MPa</br>";
                jieguo += @"
4、起爆器所在位置垂直深度静压力:"+ρgh.ToString()+"MPa</br></br></br>";
                jieguo += @"
最终结果:</br></br>
1、剪销数:"+N.ToString()+"个</br>";
                jieguo += @"
2、井口外加最小起爆压力:" + Px.ToString() + "MPa</br>";
                jieguo += @"
3、井口外加最大起爆压力:" +Ps.ToString()+"MPa</br>";
                jieguo += @"
4、井口外加计算压力:"+YL.ToString()+"MPa</br></br></br>";
                jieguo += @"
井号:" + JH + "</br>计算时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "";

                div_Content.InnerHtml = jieguo;

            }
            catch (Exception ex)
            {
                lbl_err.Text = ex.Message.ToString();
            }
        }

        //导出数据到word
        public void Download()
        {

            if (!string.IsNullOrEmpty(txt_JH.Text.Trim()))
            {
                JH = txt_JH.Text.Trim();
            }
            else
            {
                lbl_err.Text = "井号不能为空！";
                return;
            }
            if (!string.IsNullOrEmpty(txt_WJYMD.Text.Trim()))
            {
                WJYMD = txt_WJYMD.Text.Trim();
            }
            else
            {
                lbl_err.Text = "完井液密度不能为空！";
                return;
            }
            if (!string.IsNullOrEmpty(txt_JS.Text.Trim()))
            {
                JS = txt_JS.Text.Trim();
            }
            else
            {
                lbl_err.Text = "井深不能为空";
                return;
            }
            if (!string.IsNullOrEmpty(txt_AQYL.Text.Trim()))
            {
                AQYL = txt_AQYL.Text.Trim();
            }
            else
            {
                lbl_err.Text = "安全压力不能为空";
                return;
            }
            if (!string.IsNullOrEmpty(txt_WD.Text.Trim()))
            {
                WD = txt_WD.Text.Trim();
            }
            else
            {
                lbl_err.Text = "温度不能为空";
                return;
            }

            string sql = @"select JDZ from tb_eys_jqxzwd where wd=" + WD;
            DataSet ds = DbHelperOra.Query(sql.ToString());
            //剪切销值随温度降低百分率
            string WDJDS = ds.Tables[0].Rows[0]["JDZ"].ToString();
            double wdjdss = Convert.ToDouble(WDJDS);
            double wdjsss = wdjdss / 100;
            //剪切销的最小剪切值
            double Pmin = 3.09 * (1 - 0.045) * (1 - wdjsss);
            Pmin = Math.Round(Pmin, 2);
            //剪切销的最大剪切值
            double Pmax = 3.09 * (1 + 0.045) * (1 - wdjsss);
            Pmax = Math.Round(Pmax, 2);

            //起爆器所在位置垂直深度静压力
            //井深*完井液密度*0.0098
            double ρgh = Convert.ToDouble(JS) * Convert.ToDouble(WJYMD) * 0.0098;
            ρgh = Math.Round(ρgh, 2);
            //n = (P1+P安)/Pmin
            
            //剪切销钉的数量为:n=(P1+P安)/Pmin P1 = ρgh
            double N = (ρgh + Convert.ToDouble(AQYL)) / Pmin;
            double n = N;
            N = Math.Round(N, 0);
            //井口加压最大值 Ps = (N*Pmax)-P1
            double Ps = (N * Pmax) - ρgh;
            Ps = Math.Round(Ps, 2);
            //井口加压最小值        Px = (N*Pmin)-P1
            double Px = (N * Pmin) - ρgh;
            Px = Math.Round(Px, 2);
            //井口外加计算压力 YL = (Px*Ps)/2
            double YL = (Px + Ps) / 2;
            YL = Math.Round(YL, 2);



            Random rd = new Random();
            string fileName = DateTime.Now.ToString("yyyyMMddhhmm") + rd.Next() + ".doc";
            //存储路径
            string path = Server.MapPath(fileName);
            //创建字符输出流
            StreamWriter sw = new StreamWriter(path, true, System.Text.UnicodeEncoding.UTF8);
            //需要导出的内容
            string str = @"<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <title></title>
</head>
<body>
<table style='border:0px;
	padding:0;
	letter-spacing:0;
	font-size:12px;
	text-align:left;
	margin-top:5px;
	margin-bottom:5px;
	border-collapse:collapse;
	border-spacing:0; width:100%; height:100%'>
    <tr>
        <td colspan='4' align='center' style='font-size:14px; font-weight:bold;'>压力起爆装置作业参数计算软件</td>
    </tr>
    <tr>
        <td>起爆装置型号:</td>
        <td>井号:"+JH+"</td>";
            str+=@"
        <td colspan='2'>计算时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "</td>";
            str += @"
    </tr>
    <tr>
        <td colspan='4'>1. 压力起爆装置所处位置的垂直深度:"+JS+" (米)</td>";
            str += @"
    </tr>
    <tr>
        <td colspan='4'>&nbsp;&nbsp;静液柱压力P1 = pgh = "+ρgh+" (MPa)</td>";
            str += @"
    </tr>
    <tr>
        <td colspan='4'>&nbsp;&nbsp;ρ--"+WJYMD+" (g/cm^3)      g--0.0098</td>";
            str += @"
    </tr>
    <tr>
        <td colspan='4'>2.压力起爆装置所处位置的温度 t = "+WD+" (°C)</td>";
            str += @"
    </tr>
    <tr>
        <td colspan='4'>3.根据温度t查曲线，得剪切销值随温度降低百分率 Δ = "+wdjdss+"%</td>";
            str += @"
    </tr>
    <tr>
        <td colspan='4'><img src='http://localhost:58433/XDJS/quxian.jpg' /></td>
    </tr>
    <tr>
        <td colspan='4'>4. 剪切销在常温下的剪切值  τ = 3.09 ±0.139 (MPa)</td>
    </tr>
    <tr>
        <td colspan='4'>5. 计算单个剪切销在井下的最大及最小剪切值为:</td>
    </tr>
    <tr>
        <td colspan='4'>&nbsp;&nbsp;剪切销的最大剪切值 Pmax= τ×(1+4.5%)×(1-Δ%)= " + Pmax+" (MPa)</td>";
            str += @"
    </tr>
    <tr>
        <td colspan='4'>&nbsp;&nbsp;剪切销的最小剪切值 Pmin=τ×(1-4.5%)×(1-Δ%)= Pmin (MPa)</td>";
            str += @"
    </tr>
    <tr>
        <td colspan='4'>6.安全压力 P安 = "+AQYL+" (MPa)下，剪切销的数量为:</td>";
            str += @"
    </tr>
    <tr>
        <td colspan='4'>&nbsp;&nbsp;n = (P1+P安)/Pmin = "+n+"</td>";
            str += @"
    </tr>
    <tr>
        <td colspan='4'>&nbsp;&nbsp;将n向上取整得 N = "+N+",N为装配剪切销的数量</td>";
            str += @"
    </tr>
    <tr>
        <td colspan='4'>7.井口加压最大值 Ps = (N*Pmax)-P1 = "+Ps+" (MPa)</td>";
            str += @"
    </tr>
    <tr>
        <td colspan='4'>8.井口加压最小值 Px = (N*Pmin)-P1 = "+Px+" (MPa)</td>";
            str += @"
    </tr>
    <tr>
        <td colspan='4'>9.井口加压计算压力 Po = (Ps+Px)/2 = "+YL+" (MPa)</td>";
            str += @"
    </tr>
    <tr>
        <td>工程师签名:</td>
        <td></td>
        <td>甲方负责人:</td>
        <td></td>
    </tr>
    <tr>
        <td></td>
        <td>年    月    日</td>
        <td></td>
        <td>年    月    日</td>
    </tr>
</table>
</body>
</html>
";
            //写入
            sw.Write(str);
            sw.Close();
            Response.Clear();
            Response.Buffer = true;
            this.EnableViewState = false;
            Response.Charset = "utf-8";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(path);
            Response.Flush();
            Response.Close();
            Response.End();
        }

        #endregion 函数集
        
    }
}